//
//  HMHomeTableViewCell.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/24.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMHomeTableViewCell.h"
#import "HMGlobalConst.h"
#import "HMStatusToolBar.h"
#import "HMStatusRetweetView.h"
#import "HMStatusOriginalView.h"
#import <Masonry.h>

@interface HMHomeTableViewCell ()

@property (nonatomic,strong) MASConstraint *toolBarTopConstraint;

@property (nonatomic,strong)  HMStatusOriginalView *originalView;

@property (nonatomic,strong) HMStatusRetweetView *retweetView;

@property (nonatomic,strong) HMStatusToolBar *toolBar;

@end

@implementation HMHomeTableViewCell

-(void)setStatusViewModel:(HMStatusViewModel *)statusViewModel{//三个View拥有同一个viewModel
    _statusViewModel = statusViewModel;
    
    _originalView.statusViewModel = statusViewModel;
    
    _toolBar.statusViewModel = statusViewModel;
    
    [self.toolBarTopConstraint uninstall];
    
    if (statusViewModel.status.retweeted_status) {
        self.retweetView.hidden = NO;
        [self.toolBar mas_updateConstraints:^(MASConstraintMaker *make) {
            self.toolBarTopConstraint = make.top.equalTo(_retweetView.mas_bottom);
        }];
        _retweetView.statusViewModel = statusViewModel;
    }else{
        self.retweetView.hidden = YES;
        [self.toolBar mas_updateConstraints:^(MASConstraintMaker *make) {
            self.toolBarTopConstraint = make.top.equalTo(_originalView.mas_bottom);
        }];
    }
    
    
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    
    return self;
}


-(void)setupUI{
    self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    
    [self.contentView addSubview:self.originalView];
    [self.contentView addSubview:self.retweetView];
    [self.contentView addSubview:self.toolBar];
    
    [_originalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(HMHomeTableViewCellMargin);
        make.left.right.equalTo(self.contentView);
//        make.height.equalTo(@100);
        //底部约束, 在originView 内部设置,关键约束
    }];
    
    [_retweetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_originalView.mas_bottom);
        make.leading.equalTo(_originalView);
        make.trailing.equalTo(_originalView);
    }];
    
    [_toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self);
        make.trailing.equalTo(self);
        self.toolBarTopConstraint = make.top.equalTo(_retweetView.mas_bottom);
        make.height.equalTo(@35);//思考:为什么不设置底部的约束?  因为contentView的底部约束,还是不确定的.
        
        ////  关键约束 -> toolbar的底部约束等于contentView的底部约束
//        [self mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(_toolBar.mas_bottom);
//        }];
//        make.bottom.equalTo(self.contentView.mas_bottom);
        
    }];
    
    //test
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self);
        make.trailing.equalTo(self);
        make.top.equalTo(self);
        make.bottom.equalTo(_toolBar.mas_bottom);
    }];
    
}

//-(void)layoutSubviews{
//    [super layoutSubviews];
//}


#pragma mark - getter
-(HMStatusOriginalView *)originalView{
    if (_originalView == nil) {
        
        _originalView = [[HMStatusOriginalView alloc] init];
//        _originalView.backgroundColor = [self randomColor];
        
        
    }
    
    return _originalView;
}

-(HMStatusRetweetView *)retweetView{
    if (!_retweetView) {
        _retweetView = [[HMStatusRetweetView alloc] init];
    }
    return _retweetView;
}


-(HMStatusToolBar *)toolBar{
    if (!_toolBar) {
        _toolBar = [[HMStatusToolBar alloc] init];
//        _toolBar.backgroundColor = [UIColor orangeColor];
    }
    return _toolBar;
}

- (UIColor *)randomColor {
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}



@end
